package com.allinpay.merchantweb.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import com.allinpay.merchantweb.common.utils.Layui;
import com.allinpay.merchantweb.common.utils.SequenceUtil;
import com.allinpay.merchantweb.common.utils.StringUtil;
import com.allinpay.merchantweb.model.CommEnterpriseListRequest;
import com.allinpay.merchantweb.model.CommManagerListRequest;
import com.allinpay.merchantweb.model.CommRoleInfonfoList;

import com.allinpay.merchantweb.model.EnterpriseInfo;
import com.allinpay.merchantweb.model.EnterpriseInfoResponse;
import com.allinpay.merchantweb.model.Manage;
import com.allinpay.merchantweb.model.ManageAdd;
import com.allinpay.merchantweb.request.CommonReqBean;
import com.allinpay.merchantweb.response.AppCommonResponse;
import com.allinpay.merchantweb.response.ResponseData;
import com.allinpay.merchantweb.service.CommEnterpriseListService;
import com.google.gson.reflect.TypeToken;

@Service
public class CommEnterpriseListServiceImpl implements CommEnterpriseListService{

	@Autowired
	RestRequestService restRequestService;
	

	public Layui queryEnterpriseList2(CommEnterpriseListRequest commEnterpriseListRequest) throws Exception {
		// TODO Auto-generated method stub
		
		CommonReqBean<CommEnterpriseListRequest> reqBean = new CommonReqBean<CommEnterpriseListRequest>(); 
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		reqBean.setBizContent(commEnterpriseListRequest);
		
		AppCommonResponse<ResponseData<EnterpriseInfo>> respBean = restRequestService.postToAppForSingleByObj
				("/enterpriseInfo/enterpriseInfolist", reqBean
						, new TypeToken<AppCommonResponse<ResponseData<EnterpriseInfo>>>() {}.getType()
						);
		List<EnterpriseInfo> list=respBean.getData().getDataList();
		for(EnterpriseInfo p:list) {
			if(p.getDemandCycType()==1) {
				p.setDemandCycTypeStr("月");
			}else {
				p.setDemandCycTypeStr("季");
			}
		}
		
		  return this.data(respBean);
	}	

	
	public Layui addEnterprise(EnterpriseInfoResponse enterpriseInfoResponse) throws Exception {
		// TODO Auto-generated method stub
		
		CommonReqBean<EnterpriseInfoResponse> reqBean = new CommonReqBean<EnterpriseInfoResponse>(); 
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		reqBean.setBizContent(enterpriseInfoResponse);
		
		AppCommonResponse respBean = restRequestService.postToAppForSingleByObj
				("/adminUser/adduserAcct", reqBean
						, new TypeToken<AppCommonResponse>() {}.getType()
						);
		  return Layui.data1(respBean);
	}	
	
	public Layui updateManager(ManageAdd manage) throws Exception {
		// TODO Auto-generated method stub
		
		CommonReqBean<ManageAdd> reqBean = new CommonReqBean<ManageAdd>(); 
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		reqBean.setBizContent(manage);
		
		AppCommonResponse respBean = restRequestService.postToAppForSingleByObj
				("/adminUser/updUserAcct", reqBean
						, new TypeToken<AppCommonResponse>() {}.getType()
						);
		  return Layui.data1(respBean);
	}	
	
	public Layui deleteManager(Manage manage) throws Exception {
		// TODO Auto-generated method stub
		
		CommonReqBean<Manage> reqBean = new CommonReqBean<Manage>(); 
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		reqBean.setBizContent(manage);
		
		AppCommonResponse respBean = restRequestService.postToAppForSingleByObj
				("/adminUser/removeUserAcct", reqBean
						, new TypeToken<AppCommonResponse>() {}.getType()
						);
		  return Layui.data1(respBean);
	}	
	
	
    public Layui data(AppCommonResponse<ResponseData<EnterpriseInfo>> respBean){
        Layui r = new Layui();
        r.put("code", respBean.getSubCode());
        r.put("msg", respBean.getSubMsg());
        r.put("count", respBean.getData().getTotal());
        
        r.put("data", respBean.getData().getDataList());
        return r;
    }
    
    public Manage queryManagerList(CommManagerListRequest commManagerListRequest) throws Exception {
		// TODO Auto-generated method stub
    	Manage manage =new Manage();
		CommonReqBean<CommManagerListRequest> reqBean = new CommonReqBean<CommManagerListRequest>(); 
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		reqBean.setBizContent(commManagerListRequest);
		
		AppCommonResponse<ResponseData<Manage>> respBean = restRequestService.postToAppForSingleByObj
				("/adminUser/userlist", reqBean
						, new TypeToken<AppCommonResponse<ResponseData<Manage>>>() {}.getType()
						);
		if( respBean.getData().getDataList() != null&&respBean.getData().getDataList().size()>0) {
			List<Manage> list=respBean.getData().getDataList();
				String role="";
				manage=list.get(0);
				List<CommRoleInfonfoList> list1=manage.getCommRoleInfonfoList();
				for(CommRoleInfonfoList p1:list1) {
				role=role+p1.getRoleInfoId()+",";
				}
				if(!StringUtil.isEmpty(role)) {
					manage.setRole(role.substring(0, role.length()-1));
				}
			}
		  return manage;
	}

	@Override
	public Layui changePassWord(Manage manage) throws Exception {
		CommonReqBean<Manage> reqBean = new CommonReqBean<Manage>(); 
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		reqBean.setBizContent(manage);
		
		AppCommonResponse respBean = restRequestService.postToAppForSingleByObj
				("/adminUser/updUserPwd", reqBean
						, new TypeToken<AppCommonResponse>() {}.getType()
						);
		  return Layui.data1(respBean);
	}


}
